/**
 * 
 */
package appStarshipPushups.server.form;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.SpringLayout;

import appStarshipPushups.common.Protocole;
import appStarshipPushups.common.util.gui.ReverseScrollBar;
import appStarshipPushups.common.util.io.TextAreaOutputStream;
import appStarshipPushups.common.util.layout.SpringUtilities;
import appStarshipPushups.server.StarshipPushupsServer;

/**
 * Cette classe représente le contenu de l'interface du serveur.
 * @author Adam Labranche
 */
public class Content extends JPanel implements ActionListener
{
	public static final long serialVersionUID = 1L;
	
	// Couleurs de l'interface.
	private static final Color COLOR_FG = Color.white;
	private static final Color COLOR_DISABLED = Color.gray;
	
	// Textes des composantes de l'interface.
	private static final String LBL_PORT = "Port : ";
	private static final String IP_ADRESS_DESC = "Adresse IP du serveur : ";
	private static final String CMD_START = "Démarrer";
	private static final String CMD_STOP = "Arrêter";
	
	/**
	 * Largeur (en caractères) de la console dans la fenêtre.
	 */
	private static final int OUTPUT_WIDTH = 50;
	/**
	 * Hauteur (en caractères) de la console dans la fenêtre.
	 */
	private static final int OUTPUT_HEIGHT = 20;
	
	// Commandes d'action.
	private static final String ACT_START = "start";
	private static final String ACT_STOP = "stop";
		
	private JButton cmdStart = new JButton(Content.CMD_START);
	private JButton cmdStop = new JButton(Content.CMD_STOP);
	private JLabel lblPort = new JLabel(Content.LBL_PORT);
	private JTextField txtPort = new JTextField(Integer.toString(Protocole.LISTENING_PORT));
	private JLabel lblIP = new JLabel();
	private JTextArea output = new JTextArea(Content.OUTPUT_HEIGHT,
			Content.OUTPUT_WIDTH);
	private JScrollPane panOutput = null;
	
	/**
	 * C'est le construceur par défaut.
	 */
	public Content()
	{
		super();
		this.init();
	}
	
	/**
	 * Cette fonction initialise les composantes de l'interface.
	 */
	private void init()
	{
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.setOpaque(false);
		
		JPanel panEntries = new JPanel();
		panEntries.setLayout(new SpringLayout());
		panEntries.setOpaque(false);
		
		// L'étiquette qui affiche l'adresse IP du serveur.
		try
		{
			this.lblIP.setText(Content.IP_ADRESS_DESC + InetAddress.getLocalHost().getHostAddress());
		}
		catch(UnknownHostException e)
		{
			System.err.println(e.getMessage());
		}
		this.lblIP.setAlignmentX(CENTER_ALIGNMENT);
		this.lblIP.setOpaque(false);
		this.lblIP.setForeground(Content.COLOR_FG);
		this.add(lblIP);

		// L'entrée du port d'écoute du serveur.
		this.lblPort.setOpaque(false);
		this.lblPort.setForeground(Content.COLOR_FG);
		this.lblPort.setLabelFor(this.txtPort);
		panEntries.add(this.lblPort);
		panEntries.add(this.txtPort);
		this.txtPort.setDisabledTextColor(Content.COLOR_DISABLED);
		
		// Les boutons de contrôles de l'état du serveur.
		this.cmdStart.setActionCommand(Content.ACT_START);
		this.cmdStop.setActionCommand(Content.ACT_STOP);
		this.cmdStart.addActionListener(this);
		this.cmdStop.addActionListener(this);
		this.cmdStop.setEnabled(false);
		panEntries.add(this.cmdStart);
		panEntries.add(this.cmdStop);
		
		// On ajuste le panneau d'entrée grâce à un utilitaire externe.
		SpringUtilities.makeCompactGrid(panEntries, 2, 2, 6, 6, 6, 6);
		
		panEntries.setAlignmentX(CENTER_ALIGNMENT);
				
		this.add(panEntries);
		
		// La sortie du serveur.
		PrintStream out = new PrintStream(new TextAreaOutputStream(this.output));
		System.setOut(out);
		System.setErr(out);
		this.output.setEditable(false);
		this.panOutput = new JScrollPane(this.output,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		this.panOutput.setVerticalScrollBar(new ReverseScrollBar());
		this.panOutput.setAlignmentX(CENTER_ALIGNMENT);

		this.add(this.panOutput);
	}
			
    /**
     * Appelée lorsqu'une action se produit.
     */
    public void actionPerformed(ActionEvent e)
    {
    	String actCmd = e.getActionCommand();
    	if(actCmd.equals(Content.ACT_START))
    	{
    		this.txtPort.setEnabled(false);
    		this.cmdStart.setEnabled(false);
    		this.cmdStop.setEnabled(true);
    		StarshipPushupsServer.getInstance().setPort(Integer.parseInt(this.txtPort.getText()));
    		StarshipPushupsServer.getInstance().start();
    	}
    	else if(actCmd.equals(Content.ACT_STOP))
    	{
    		StarshipPushupsServer.getInstance().stopServer();
    		this.txtPort.setEnabled(true);
    		this.cmdStart.setEnabled(true);
    		this.cmdStop.setEnabled(false);
    	}
    }
}
